﻿using System.Linq;
using System.Web.Mvc;
using CultuRAM.DAL;
using CultuRAM.Models;
using PagedList;

namespace CultuRAM.Controllers
{
    [Authorize]
    public class ParametersController : Controller
    {

        private readonly UnitOfWork _unitOfWork = new UnitOfWork();
        private static int _idParam, _page;
        //private static MvcHtmlString _msgInfo, _msgSuccess, _msgWarning, _msgError;

        //
        // GET: /Parameters/

        public ActionResult Index(int id = 0)
        {

            PopulateParametersDropDownList(id);
            return View();
        }

        //
        // GET: /Parameters/DefaultRight

        public ActionResult DefaultRight(int idParam = 0)
        {
            return PartialView("_DefaultRight");
        }

        //
        // GET: /Parameters/ListParameters

        public ActionResult ListParameters(int idParam = 0, int page = 0)
        {

            if (!Request.HttpMethod.Equals("GET"))
            {
                page = 1;
            }

            const int pageSize = 7;
            int pageNumber;

            if (_page == 0)
            {
                pageNumber = page == 0 ? 1 : page;
            }
            else
            {
                pageNumber = _page;
            }
            _page = 0;

            idParam = _idParam == 0 ? idParam : _idParam;
            _idParam = 0;

            ViewBag.IdParam = idParam;

            if (idParam == 0)
            {
                ViewBag.MsgWarning = new MvcHtmlString("<b>ATENÇÃO:</b><br> Selecione um Parâmetro!");

            }

            //ViewBag.MsgSuccess = _msgSuccess;
            //ViewBag.MsgError = _msgError;
            //ViewBag.MsgWarning = _msgWarning;
            //ViewBag.MsgInfo = _msgInfo;
            //_msgSuccess = new MvcHtmlString(String.Empty);
            //_msgError = new MvcHtmlString(String.Empty);
            //_msgInfo = new MvcHtmlString(String.Empty);
            //_msgWarning = new MvcHtmlString(String.Empty);
           
            switch (idParam)
            {
                case 1:
                    var model1 = _unitOfWork.RolesRepository.Get().ToList();
                    return PartialView("_ListUserRoles", model1.ToPagedList(pageNumber, pageSize));
                case 2:
                    var model2 = _unitOfWork.EventTypeRepository.Get().ToList();
                    return PartialView("_ListEventTypes", model2.ToPagedList(pageNumber, pageSize));
                case 3:
                    var model3 = _unitOfWork.ResourceTypeRepository.Get().ToList();
                    return PartialView("_ListResourceTypes", model3.ToPagedList(pageNumber, pageSize));
                case 4:
                    var model4 = _unitOfWork.EntityTypeRepository.Get().ToList();
                    return PartialView("_ListEntityTypes", model4.ToPagedList(pageNumber, pageSize));
                case 5:
                    var model5 = _unitOfWork.ParticipantTypeRepository.Get().ToList();
                    return PartialView("_ListParticipType", model5.ToPagedList(pageNumber, pageSize));
                case 6:
                    var model6 = _unitOfWork.AccStateRepository.Get().ToList();
                    return PartialView("_ListAccState", model6.ToPagedList(pageNumber, pageSize));
                case 7:
                    var model7 = _unitOfWork.EventStateRepository.Get().ToList();
                    return PartialView("_ListEventState", model7.ToPagedList(pageNumber, pageSize));
                case 8:
                    var model8 = _unitOfWork.CityRepository.Get().ToList();
                    return PartialView("_ListCities", model8.ToPagedList(pageNumber, pageSize));
                case 9:
                    var model9 = _unitOfWork.PlaceRepository.Get().ToList();
                    return PartialView("_ListPlaces", model9.ToPagedList(pageNumber, pageSize));
                case 10:
                    var model10 = _unitOfWork.ParticipantRepository.Get().ToList();
                    return PartialView("_ListParticipants", model10.ToPagedList(pageNumber, pageSize));
                case 11:
                    var model11 = _unitOfWork.EventPropertyRepository.Get().ToList();
                    return PartialView("_ListEventProperties", model11.ToPagedList(pageNumber, pageSize));
                default:
                    return PartialView("_DefaultLeft");
            }  
        }
        

        //
        // GET: /Parameters/CreateGeneric

        public ActionResult CreateGeneric(int page = 0, int idParam = 0)
        {

            var p = new Parameter
                {
                    IdParam = idParam != 0 ? idParam : _idParam
                };

            ViewBag.idParam = p.IdParam;

            ViewBag.Page = _page > 0 ? _page : page;

            PopulateParticipTypeDropDownList();
            PopulateEventTypeDropDownList();
            PopulateEventPropertyKindDropDownList("");

            return PartialView("_CreateGeneric", p);
        }

        //
        // POST: /Parameters/CreateGeneric

        [HttpPost]
        public ActionResult CreateGeneric(Parameter parameter, int page = 0, int idParam = 0)
        {

            ViewBag.idParam = idParam;
            
            if (ModelState.IsValid)
            {
                switch (idParam)
                {
                    case 1:
                        if (_unitOfWork.RolesRepository.Get().All(p => p.RoleName != parameter.WebpagesRole.RoleName))
                        {
                            _unitOfWork.RolesRepository.Insert(parameter.WebpagesRole);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    case 2:
                        PopulateEventTypeDropDownList(parameter.EventType.SuperTypeId);
                        if (_unitOfWork.EventTypeRepository.Get().All(p => p.Type != parameter.EventType.Type))
                        {
                            _unitOfWork.EventTypeRepository.Insert(parameter.EventType);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    case 3:
                        if (_unitOfWork.ResourceTypeRepository.Get().All(p => p.ResType != parameter.ResourceType.ResType))
                        {
                            _unitOfWork.ResourceTypeRepository.Insert(parameter.ResourceType);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    case 4:
                        if (_unitOfWork.EntityTypeRepository.Get().All(p => p.EntityTypeName != parameter.EntityType.EntityTypeName))
                        {
                            _unitOfWork.EntityTypeRepository.Insert(parameter.EntityType);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    case 5:
                        if (_unitOfWork.ParticipantTypeRepository.Get().All(p => p.ParticipType != parameter.ParticipantType.ParticipType))
                        {
                            _unitOfWork.ParticipantTypeRepository.Insert(parameter.ParticipantType);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    case 6:
                        if (_unitOfWork.AccStateRepository.Get().All(p => p.AccState != parameter.AccreditingState.AccState))
                        {
                            _unitOfWork.AccStateRepository.Insert(parameter.AccreditingState);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    case 7:
                        if (_unitOfWork.EventStateRepository.Get().All(p => p.State != parameter.EventState.State))
                        {
                            _unitOfWork.EventStateRepository.Insert(parameter.EventState);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    case 8:
                        if (_unitOfWork.CityRepository.Get().All(p => p.CityName != parameter.City.CityName))
                        {
                            _unitOfWork.CityRepository.Insert(parameter.City);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    case 9:
                        if (_unitOfWork.PlaceRepository.Get().All(p => p.NamePlace != parameter.Place.NamePlace))
                        {
                            _unitOfWork.PlaceRepository.Insert(parameter.Place);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    case 10:
                        PopulateParticipTypeDropDownList(parameter.Participant.ParticipantTypeId);
                        if (_unitOfWork.ParticipantRepository.Get().All(p => p.Name != parameter.Participant.Name))
                        {
                            _unitOfWork.ParticipantRepository.Insert(parameter.Participant);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    case 11:
                        PopulateEventPropertyKindDropDownList(parameter.EventProperty.Kind);
                        if (_unitOfWork.EventPropertyRepository.Get().All(p => p.Name != parameter.EventProperty.Name))
                        {
                            _unitOfWork.EventPropertyRepository.Insert(parameter.EventProperty);
                            _unitOfWork.Save();
                        }
                        else
                        {
                            ViewBag.MsgError = new MvcHtmlString("Já existe um registo com essa designação!");
                            return PartialView("_DefaultRight");
                        }
                        break;
                    default:
                        return PartialView("_DefaultRight");
                }

                _idParam = idParam;

                ViewBag.MsgSucess = new MvcHtmlString("O registo foi efetuado com Sucesso!");
                return PartialView("_DefaultRight");
                //return RedirectToAction("CreateGeneric");
            }

            ViewBag.MsgError = new MvcHtmlString("Ocorreu um erro ao guardar o novo registo. Tente novamente!");
            return PartialView("_CreateGeneric", parameter);

        }

        //
        // GET: /Parameters/EditGeneric

        public ActionResult EditGeneric(int page = 0, int idParam = 0, int id = 0)
        {
            var parameter = new Parameter
                {
                    IdParam = idParam != 0 ? idParam : _idParam
                };

            ViewBag.idParam = parameter.IdParam;
            ViewBag.Page = _page > 0 ? _page : page;

            _idParam = idParam;

            if (id != 0)
            {
                if (ModelState.IsValid)
                {
                    switch (idParam)
                    {
                        case 1:
                            parameter.WebpagesRole = _unitOfWork.RolesRepository.GetByID(id);
                            break;
                        case 2:
                            parameter.EventType = _unitOfWork.EventTypeRepository.GetByID(id);
                            PopulateEventTypeDropDownList(parameter.EventType.SuperTypeId);
                            break;
                        case 3:
                            parameter.ResourceType = _unitOfWork.ResourceTypeRepository.GetByID(id);
                            break;
                        case 4:
                            parameter.EntityType = _unitOfWork.EntityTypeRepository.GetByID(id);
                            break;
                        case 5:
                            parameter.ParticipantType = _unitOfWork.ParticipantTypeRepository.GetByID(id);
                            break;
                        case 6:
                            parameter.AccreditingState = _unitOfWork.AccStateRepository.GetByID(id);
                            break;
                        case 7:
                            parameter.EventState = _unitOfWork.EventStateRepository.GetByID(id);
                            break;
                        case 8:
                            parameter.City = _unitOfWork.CityRepository.GetByID(id);
                            break;
                        case 9:
                            parameter.Place = _unitOfWork.PlaceRepository.GetByID(id);
                            break;
                        case 10:
                            parameter.Participant = _unitOfWork.ParticipantRepository.GetByID(id);
                            PopulateParticipTypeDropDownList(parameter.Participant.ParticipantTypeId);
                            break;
                        case 11:
                            parameter.EventProperty = _unitOfWork.EventPropertyRepository.GetByID(id);
                            PopulateEventPropertyKindDropDownList(parameter.EventProperty.Kind);
                            break;
                        default:
                            return PartialView("_DefaultRight");
                    }  

                    return PartialView("_EditGeneric", parameter);
                }

            } //If idParam == 0
            else
            {
                ViewBag.MsgWarning = new MvcHtmlString("Tem de selecionar um registo!");
                return PartialView("_DefaultRight");
                //return RedirectToAction("ListParameters");
                
            }
            return PartialView("_DefaultRight");
            //return RedirectToAction("ListParameters");
        }

        //
        // POST: /Parameters/EditGeneric
        
        [HttpPost]
        public ActionResult EditGeneric(Parameter parameter, int idParam)
        {
            
            ViewBag.idParam = idParam;


                if (ModelState.IsValid)
                {
                    switch (idParam)
                    {
                        case 1:
                            _unitOfWork.RolesRepository.Update(parameter.WebpagesRole);
                            _unitOfWork.Save();
                            break;
                        case 2:
                            var objTemp = _unitOfWork.EventTypeRepository.GetByID(parameter.EventType.TypeId);
                            PopulateEventTypeDropDownList(parameter.EventType.SuperTypeId);
                            objTemp.Type = parameter.EventType.Type;
                            objTemp.Color = parameter.EventType.Color;
                            objTemp.SuperTypeId = parameter.EventType.SuperTypeId;
                            _unitOfWork.EventTypeRepository.UpdateV0(objTemp);
                            _unitOfWork.Save();
                            break;
                        case 3:
                            _unitOfWork.ResourceTypeRepository.Update(parameter.ResourceType);
                            _unitOfWork.Save();
                            break;
                        case 4:
                            _unitOfWork.EntityTypeRepository.Update(parameter.EntityType);
                            _unitOfWork.Save();
                            break;
                        case 5:
                            _unitOfWork.ParticipantTypeRepository.Update(parameter.ParticipantType);
                            _unitOfWork.Save();
                            break;
                        case 6:
                            _unitOfWork.AccStateRepository.Update(parameter.AccreditingState);
                            _unitOfWork.Save();
                            break;
                        case 7:
                            _unitOfWork.EventStateRepository.Update(parameter.EventState);
                            _unitOfWork.Save();
                            break;
                        case 8:
                            _unitOfWork.CityRepository.Update(parameter.City);
                            _unitOfWork.Save();
                            break;
                        case 9:
                            _unitOfWork.PlaceRepository.Update(parameter.Place);
                            _unitOfWork.Save();
                            break;
                        case 10:
                            PopulateParticipTypeDropDownList(parameter.Participant.ParticipantTypeId);
                            _unitOfWork.ParticipantRepository.Update(parameter.Participant);
                            _unitOfWork.Save();
                            break;
                        case 11:
                            PopulateEventPropertyKindDropDownList(parameter.EventProperty.Kind);
                            _unitOfWork.EventPropertyRepository.Update(parameter.EventProperty);
                            _unitOfWork.Save();
                            break;
                    }

                    _idParam = idParam;

                    ViewBag.MsgSuccess = new MvcHtmlString("As alterações foram guardadas com Sucesso!");
                    return PartialView("_DefaultRight");
                   // return RedirectToAction("ListParameters");
                }

                ViewBag.MsgError = new MvcHtmlString("Ocorreu um erro ao guardar as alterações. Tente novamente!");
                return PartialView("_EditGeneric", parameter);

        }


        //
        // GET: /Parameters/Delete

        public ActionResult DeleteGeneric(int page = 0, int idParam = 0, int id = 0)
        {
            if (id == 0)
            {
                ViewBag.MsgWarning = new MvcHtmlString("Tem de selecionar um registo!");
                return PartialView("_DefaultRight");
            }
            
            var parameter = new Parameter();

            parameter.IdParam = idParam != 0 ? idParam : _idParam;

            ViewBag.idParam = parameter.IdParam;
            ViewBag.Page = _page > 0 ? _page : page;

            if (ModelState.IsValid)
            {
                switch (idParam)
                {
                    case 1:
                        parameter.WebpagesRole = _unitOfWork.RolesRepository.GetByID(id);
                        break;
                    case 2:
                        parameter.EventType = _unitOfWork.EventTypeRepository.GetByID(id);
                        PopulateEventTypeDropDownList(parameter.EventType.SuperTypeId);
                        break;
                    case 3:
                        parameter.ResourceType = _unitOfWork.ResourceTypeRepository.GetByID(id);
                        break;
                    case 4:
                        parameter.EntityType = _unitOfWork.EntityTypeRepository.GetByID(id);
                        break;
                    case 5:
                        parameter.ParticipantType = _unitOfWork.ParticipantTypeRepository.GetByID(id);
                        break;
                    case 6:
                        parameter.AccreditingState = _unitOfWork.AccStateRepository.GetByID(id);
                        break;
                    case 7:
                        parameter.EventState = _unitOfWork.EventStateRepository.GetByID(id);
                        break;
                    case 8:
                        parameter.City = _unitOfWork.CityRepository.GetByID(id);
                        break;
                    case 9:
                        parameter.Place = _unitOfWork.PlaceRepository.GetByID(id);
                        break;
                    case 10:
                        parameter.Participant = _unitOfWork.ParticipantRepository.GetByID(id);
                        PopulateParticipTypeDropDownList(parameter.Participant.ParticipantTypeId);
                        break;
                    case 11:
                        parameter.EventProperty = _unitOfWork.EventPropertyRepository.GetByID(id);
                        PopulateEventPropertyKindDropDownList(parameter.EventProperty.Kind);
                        break;
                }

                _idParam = idParam;

                return PartialView("_DeleteGeneric", parameter);     

            }

            return PartialView("_DeleteGeneric", parameter);

        }

        //
        // POST: /Parameters/DeleteGeneric

        [HttpPost]
        public ActionResult DeleteGeneric(Parameter parameter, int page = 0, int idParam = 0)
        {
           
            ViewBag.idParam = idParam;
            _page = page;

            if (ModelState.IsValid)
            {
                int result;
                switch (idParam)
                {
                    case 1:
                        result = _unitOfWork.UserInRolesRepository.Get(
                            filter: x => x.RoleId == parameter.WebpagesRole.RoleId).Count();

                        if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " + result +
                                        " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }

                        _unitOfWork.RolesRepository.Delete(parameter.WebpagesRole);
                        _unitOfWork.Save();
                            
                        break;   
                    case 2:
                        var objTemp = _unitOfWork.EventTypeRepository.GetByID(parameter.EventType.TypeId);
                       result = _unitOfWork.EventTypeRepository.Get(
                            filter: x => x.SuperTypeId == parameter.EventType.TypeId).Count();

                        if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " + result +
                                        " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }

                        PopulateEventTypeDropDownList(parameter.EventType.SuperTypeId);
                        _unitOfWork.EventTypeRepository.Delete(objTemp);
                        _unitOfWork.Save();
                    
                        break;
                    case 3:
                        result = _unitOfWork.ResourceRepository.Get(
                            filter: x => x.ResTypeId == parameter.ResourceType.ResTypeId).Count();

                        if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " 
                                            + result + " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }

                        _unitOfWork.ResourceTypeRepository.Delete(parameter.ResourceType);
                        _unitOfWork.Save();
                      
                        break;
                    case 4:
                        result = _unitOfWork.EntityRepository.Get(
                            filter: x => x.EntityTypeId == parameter.EntityType.EntityTypeId).Count();

                        if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " 
                                         + result + " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }
                        _unitOfWork.EntityTypeRepository.Delete(parameter.EntityType);
                        _unitOfWork.Save();
                     
                        break;
                    case 5:
                        result = _unitOfWork.ParticipantRepository.Get(
                            filter: x => x.ParticipantTypeId == parameter.ParticipantType.ParticipTypeId).Count();

                        if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " 
                                + result + " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }

                        _unitOfWork.ParticipantTypeRepository.Delete(parameter.ParticipantType);
                        _unitOfWork.Save();

                        break;
                    case 6:
                        result = _unitOfWork.AccResquestRepository.Get(
                            filter: x => x.AccStateId == parameter.AccreditingState.AccStateId).Count();

                        if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " 
                                + result + " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }

                        _unitOfWork.AccStateRepository.Delete(parameter.AccreditingState);
                        _unitOfWork.Save();
                        
                        break;
                    case 7:
                        //REVER ******************
                        //result = _unitOfWork.EventRepository.Get(filter: x => x.GetPropertyValue("Estado")));
                        result = 0;

                        if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " 
                                + result + " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }
                        
                        _unitOfWork.EventStateRepository.Delete(parameter.EventState);
                        _unitOfWork.Save();
                       
                        break;
                    case 8:
                        result = _unitOfWork.EntityRepository.Get(
                            filter: x => x.CityId == parameter.City.CityId).Count();

                        if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " + result +
                                        " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }

                        _unitOfWork.CityRepository.Delete(parameter.City);
                        _unitOfWork.Save();

                        break;
                    case 9:
                        //REVER ******************
                        //result = _unitOfWork.EventRepository.Get(filter: x => x.GetPropertyValue("Estado")));
                        result = 0;

                       if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " 
                                + result + " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }

                        _unitOfWork.PlaceRepository.Delete(parameter.Place);
                        _unitOfWork.Save();
                       
                        break;
                    case 10:
                        result = _unitOfWork.EventParticipantRepository.Get(
                            filter: x => x.ParticipantId == parameter.Participant.ParticipId).Count();

                        if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " 
                                + result + " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }

                        PopulateParticipTypeDropDownList(parameter.Participant.ParticipantTypeId);
                        _unitOfWork.ParticipantRepository.Delete(parameter.Participant);
                        _unitOfWork.Save();
                       
                        break;
                    case 11:
                        result = _unitOfWork.EventPropertyValueRepository.Get(
                            filter: x => x.EventPropertyId == parameter.EventProperty.PropertyId).Count();

                        if (result > 0)
                        {
                            ViewBag.MsgError = new MvcHtmlString("O registo não pode ser eliminado, porque está a ser utilizado por " 
                                + result + " registo(s)!");
                            return PartialView("_DefaultRight");
                            //return RedirectToAction("ListParameters");
                        }

                        PopulateEventPropertyKindDropDownList(parameter.EventProperty.Kind);
                        _unitOfWork.EventPropertyRepository.Delete(parameter.EventProperty);
                        _unitOfWork.Save();

                        break;
                    default:
                        ViewBag.MsgError = new MvcHtmlString("Ocorreu um erro ao tentar eliminar o registo. Tente novamente!");
                        return PartialView("_DefaultRight");
                }

                _idParam = idParam;

                ViewBag.MsgSuccess = new MvcHtmlString("O registo foi eliminado com Sucesso!");
                return PartialView("_DefaultRight");
                //return RedirectToAction("ListParameters");
            }

            return PartialView("_DeleteGeneric", parameter);
        }


        //AUXILIARY METHODS /////////////////////////////////////////////////////////

        //Method: Populate the DropDownList of Entity Type
        private void PopulateParametersDropDownList(int id)
        {
            ViewBag.Parameters = new SelectList(new[]
                {
                    new SelectListItem {Value = "0", Text = ""},
                    new SelectListItem {Value = "1", Text = "Grupos de Utilizador"},
                    new SelectListItem {Value = "2", Text = "Tipos de Evento"},
                    new SelectListItem {Value = "3", Text = "Tipos de Recurso"},
                    new SelectListItem {Value = "4", Text = "Tipos de Entidade"},
                    new SelectListItem {Value = "5", Text = "Tipos de Artistas / Grupos Artísticos"},
                    new SelectListItem {Value = "6", Text = "Estados do Pedido de Adesão"},
                    new SelectListItem {Value = "7", Text = "Estados do Evento"},
                    new SelectListItem {Value = "8", Text = "Municípios"},
                    new SelectListItem {Value = "9", Text = "Locais"},
                    new SelectListItem {Value = "10", Text = "Artístas / Grupos Artísticos"},
                    new SelectListItem {Value = "11", Text = "Propriedades do Evento"}
                }, "Value", "Text", id).OrderBy(x => x.Text);
        }
        
        //Method: Populate the DropDownList of Entity Type
        private void PopulateEventPropertyKindDropDownList(string kind)
        {
            ViewBag.Kind = new SelectList(new[]
                {
                    new SelectListItem {Value = "", Text = ""},
                    new SelectListItem {Value = "string", Text = "Texto"},
                    new SelectListItem {Value = "int", Text = "Número Inteiro"},
                    new SelectListItem {Value = "double", Text = "Número Décimal"},
                    new SelectListItem {Value = "double", Text = "Moeda"},
                    new SelectListItem {Value = "bool", Text = "Sim/Não"}
                }, "Value", "Text", kind);
        }

        //Method: Populate the DropDownList of Participant Types
        private void PopulateParticipTypeDropDownList(int? selectedParticipTypeId = null)
        {
            if (selectedParticipTypeId == null)
            {
                var participTypes = _unitOfWork.ParticipantTypeRepository.GetList();
                ViewBag.ParticipantTypeId = new SelectList(participTypes, "ParticipTypeId", "ParticipType");
            }
            else
            {
                var participTypes = _unitOfWork.ParticipantTypeRepository.GetList();
                ViewBag.ParticipantTypeId = new SelectList(participTypes, "ParticipTypeId", "ParticipType", selectedParticipTypeId);
            }
        }

        //Method: Populate the DropDownList of Event Types
        private void PopulateEventTypeDropDownList(int? selectedEventTypeId = null)
        {
            if (selectedEventTypeId == null)
            {
                var eeventTypes = _unitOfWork.EventTypeRepository.GetList();
                ViewBag.SuperTypeId = new SelectList(eeventTypes, "TypeId", "Type");
            }
            else
            {
                var eeventTypes = _unitOfWork.EventTypeRepository.GetList();
                ViewBag.SuperTypeId = new SelectList(eeventTypes, "TypeId", "Type", selectedEventTypeId);
            }
        }

    }//End Controller   
}

